<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var zTreeObj;
// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
		 /* async: {
	        enable: true,
	        url: '$!{webPath}/tssy/tsSyResource/loadTreeData',
	        contentType: "application/json",
	        type:"get",
	        autoParam: ["id"]
	    }  */
	    callback:{
	    	onClick:zTreeOnClick
	    } 
};
// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
function initTree(){
	 $.ajax({
         url: '$!{webPath}/tssy/tsSyResource/loadTreeData',
         type: 'get',
         data: {"pid":"0"},
         dataType: 'json',
         async:false,
         success: function(d){
        	 zTreeObj = $.fn.zTree.init($.CurrentNavtab.find("#treeResources"), setting, d);
        	 zTreeObj.expandAll(true); 
         }
     });
}

var checkId;
var resourceType;
function zTreeOnClick(event,treeId,treeNode){
	checkId = treeNode.id;
	resourceType = treeNode.resourceType;
	setDataToForm(treeNode);
}

function setDataToForm(treeNode){
	ajaxGetInfo(treeNode.id);
}

function ajaxGetInfo(id){
	 $.ajax({
         url: '$!{webPath}/tssy/tsSyResource/qryRecordInfoForAjax',
         type: 'get',
         data: {"id":id},
         dataType: 'json',
         async:false,
         success: function(d){
        	var recordObj = d.record;
        	$.CurrentNavtab.find("#resourceForm #id").val(recordObj.id);
        	$.CurrentNavtab.find("#resourceForm #resourceName").val(recordObj.resourceName);
        	$.CurrentNavtab.find("#resourceForm #remark").val(recordObj.remark);
        	$.CurrentNavtab.find("#resourceForm #pid").val(recordObj.pid);
       		if(recordObj.resourceType=="MENU"){
       			$.CurrentNavtab.find("#resourceForm input[type='radio'][name='resourceType'][value='MENU']").attr("checked","checked");
       			$.CurrentNavtab.find("#resourceForm #url").val(recordObj.url);
           		if(recordObj.isExtend=="T"){
           			$.CurrentNavtab.find("#resourceForm input[type='radio'][name='isExtend'][value='T']").attr("checked","checked");
           		}else{
           			$.CurrentNavtab.find("#resourceForm input[type='radio'][name='isExtend'][value='F']").attr("checked","checked");
           		}
           		$.CurrentNavtab.find("#resourceForm #rel").val(recordObj.rel);
           		$.CurrentNavtab.find("#codeTr").hide();
           		$.CurrentNavtab.find("#urlTr").show();
           		$.CurrentNavtab.find("#isExtendTr").show();
       		}else if(recordObj.resourceType=="BUTTON"){
       			$.CurrentNavtab.find("#codeTr").show();
       			$.CurrentNavtab.find("#urlTr").hide();
       			$.CurrentNavtab.find("#isExtendTr").hide();
       			$.CurrentNavtab.find("#resourceForm input[type='radio'][name='resourceType'][value='BUTTON']").attr("checked","checked");
       			$.CurrentNavtab.find("#resourceForm #code").val(recordObj.code);
       		}
       		if(allResource){
       			$.each(allResource,function(key,value){
       				if(value.id==recordObj.pid){
       					$.CurrentNavtab.find("#resourceForm #pname").val(value.resourceName);
       				}
       			});
       		}
         }
     });
}

function changeType(){
	if($.CurrentNavtab.find("#resourceForm input[name='resourceType']:checked").val()=="MENU"){
		$.CurrentNavtab.find("#codeTr").hide();
		$.CurrentNavtab.find("#urlTr").show();
		$.CurrentNavtab.find("#isExtendTr").show();
	 }else if($.CurrentNavtab.find("#resourceForm input[name='resourceType']:checked").val()=="BUTTON"){
		 $.CurrentNavtab.find("#codeTr").show();
		 $.CurrentNavtab.find("#urlTr").hide();
		 $.CurrentNavtab.find("#isExtendTr").hide();
	 }
}

var allResource;
function querryAll(){
	$.ajax({
        url: '$!{webPath}/tssy/tsSyResource/querryAll',
        type: 'get',
        dataType: 'json',
        async:false,
        success: function(d){
        	allResource = d;
        }
    });
}

function saveRecord(){
	if($.CurrentNavtab.find("#resourceForm #pid").val()){
		 if($.CurrentNavtab.find("#resourceForm input[name='resourceType']:checked").val()=="MENU"){
			 $.CurrentNavtab.find('#resourceForm').validator('setField', {
					    code: null      //将不再验证code字段
				});
			 }else if($.CurrentNavtab.find("#resourceForm input[name='resourceType']:checked").val()=="BUTTON"){
				 $.CurrentNavtab.find('#resourceForm').validator('setField', {
					    code: 'required' 
				});
			 }
		 $.CurrentNavtab.find("#resourceForm").submit();
	}else{
		alert("请先选择资源");
		return false;
	}
}

function resetForm(){
	$.CurrentNavtab.find("#resourceForm")[0].reset();
}

var saveFlag = "edit";
function addTj(){
	if(checkId){
		var node = zTreeObj.getNodeByParam("id", checkId, null);
		resetForm();
		$.CurrentNavtab.find("#resourceForm #id").val("");
		$.CurrentNavtab.find("#resourceForm #pid").val(node.pid);
		if(allResource){
   			$.each(allResource,function(key,value){
   				if(value.id==node.pid){
   					$.CurrentNavtab.find("#resourceForm #pname").val(value.resourceName);
   				}
   			});
   		}
		saveFlag = "addTj";
	}else{
		alert("请先选择资源");
		return false;
	}
}

function addXj(){
	if(checkId){
		var node = zTreeObj.getNodeByParam("id", checkId, null);
		if(resourceType=="MENU"){
			resetForm();
			$.CurrentNavtab.find("#resourceForm #id").val("");
			$.CurrentNavtab.find("#resourceForm #pid").val(checkId);
			$.CurrentNavtab.find("#resourceForm #pname").val(node.name);
			saveFlag = "addXj";
		 }else if(resourceType=="BUTTON"){
			 alert("不能给按钮加下级资源");
			 return false;
		 }
	}else{
		alert("请先选择资源");
		return false;
	}
}

function delRecord(){
	if($.CurrentNavtab.find("#resourceForm #id").val()){
		$.ajax({
	        url: '$!{webPath}/tssy/tsSyResource/delRecord',
	        type: 'post',
	        dataType: 'json',
	        async:false,
	        data:{"id":$.CurrentNavtab.find("#resourceForm #id").val(),"_method":"delete"},
	        success: function(d){
	        	var recordObj = d.record;
	        	var node = zTreeObj.getNodeByParam("id", recordObj.id, null);
	        	zTreeObj.removeNode(node);
	        	resetForm();
	        }
	    });
	}else{
		alert("请先选择资源");
		return false;
	}
}

$(document).ready(function(){
	$.CurrentNavtab.find("#codeTr").hide();
	querryAll();
	initTree();
	$.CurrentNavtab.find('#resourceForm').validator({
    	onsubmit:true,// 是否在提交是验证  
	    rules: {
	       
	    },
	    fields: {
	    	resourceName:"required"
	    },  
	    // 表单验证通过后，ajax提交
	    valid: function(form){
	    	$.CurrentNavtab.find('#resourceForm').ajaxSubmit({  
				url: '$!{webPath}/tssy/tsSyResource/saveRecord',
			    type: 'post',
			    dataType: 'json',
		        success:function(data){
		        	    var recordObj = data.record;
			       		//待加入到树形...
			       		if(saveFlag == "addTj"){
			       			var pnode = zTreeObj.getNodeByParam("id", recordObj.pid, null);
			       			var node = {"id":recordObj.id,"name":recordObj.resourceName,"pid":recordObj.pid,"resourceType":recordObj.resourceType};
				       		node = zTreeObj.addNodes(pnode,node,true);
			       		}else if(saveFlag == "addXj"){
			       			var pnode = zTreeObj.getNodeByParam("id", checkId, null);
			       			var node = {"id":recordObj.id,"name":recordObj.resourceName,"pid":recordObj.pid,"resourceType":recordObj.resourceType};
				       		node = zTreeObj.addNodes(pnode,node,true);
			       		}else{
			       			var node = zTreeObj.getNodeByParam("id", recordObj.id, null);
			       		    node.name = recordObj.resourceName;
			       		    node.resourceType = recordObj.resourceType;
			       			zTreeObj.updateNode(node);
			       		}
			       		querryAll();
			       		resetForm();
			       		checkId = "";
			       		saveFlag = "";
		         }  
		     });  
	    }
	});
});
  
</script>
<div class="bjui-pageHeader">
	<div class="pull-right">
		<button type="button" class="btn-blue" data-icon="plus"
			onclick="addTj()">新增同级</button>
		&nbsp;
		<button type="button" class="btn-blue" data-icon="plus"
			onclick="addXj()">新增子级</button>
		&nbsp;
		<button type="button" class="btn-blue" data-icon="save"
			onclick="saveRecord()">保存</button>
		&nbsp;
		<button type="button" class="btn-blue" data-icon="remove"
			onclick="delRecord()">删除</button>
		&nbsp;
	</div>
</div>
<div class="bjui-pageContent tableContent">
	<div style="width: 20%; float: left;">
		<ul id="treeResources" class="ztree"></ul>
	</div>
	<div style="width: 70%; float: left;">
		<div>
			<!-- 查询和编辑公用一个页面，id空：新增，id不空：编辑--
		注意：本系统遵循restful架构，action处理时method严格按照 新增：post 编辑：put 删除:delete 查询：get(默认)
		由于删除和编辑form表单不支持，所以必须在action的url后面增加参数_method=put或_method=delete
	-->
			<form id="resourceForm" type="post" data-alertmsg="false">
				<input type="hidden" id="id" name="id" value="" /> <input
					type="hidden" name="pid" id="pid" value="" />
				<table class="table table-condensed table-hover" width="100%">
					<tbody>
						<tr>
							<td><label for="j-resourceType" class="control-label x85">资源类型：</label>
								<input type="radio" name="resourceType"
								opertion="EqualTo" value="MENU" onclick="changeType()"
								#if($!record.resourceType&&$record.resourceType==
								'MENU')checked#end id="jm-resourceType" class="form-control" /><label
								for="jm-resourceType">&nbsp;菜单</label> &nbsp;&nbsp;&nbsp;
								<input type="radio"
								name="resourceType" opertion="EqualTo" onclick="changeType()"
								value="BUTTON" #if($!record.resourceType&&$record.resourceType==
								'BUTTON')checked#end id="jb-resourceType" class="form-control" /><label
								for="jb-resourceType">&nbsp;按钮</label></td>
									<td><label for="j-resourceName" class="control-label x85">资源名称：</label>
								<input type="text" name="resourceName" id="resourceName"
								maxlength=200 value="" size="15" id="j-resourceName" /></td>
						</tr>
						<tr>
						     <td><label for="j-pid" class="control-label x85">上级资源：</label>
								<input readonly type="text" name="pname" id="pname" value=""
								size="15" id="j-pid" /></td>
						      <td><label for="j-remark" class="control-label x85">备注：</label>
								<input type="text" name="remark" id="remark" value="" size="25"
								id="j-remark" /></td>

						</tr>
						<tr id="codeTr">
							<td><label for="j-code" class="control-label x85">按钮code：</label>
								<input type="text" name="code" id="code" value="$!record.code" size="15"
								id="j-code" /></td>
						</tr>
						<tr id="urlTr">
							<td><label for="j-url" class="control-label x85">url：</label>
								<input type="text" name="url" id="url" value="" size="40"
								maxlength=200 id="j-url" /></td>
							<td><label for="j-rel" class="control-label x85">rel：</label>

								<input type="text" name="rel" id="rel" value="" size="25"
								id="j-rel" /></td>
						</tr>
						<tr id="isExtendTr">
							<td><label class="control-label x85">系统外链接：</label> <input
								type="radio" class="form-control" name="isExtend" value="T" /><label
								for="j1-isExtend">&nbsp;是</label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
								type="radio" class="form-control" name="isExtend" value="F" /><label
								for="j2-isExtend">&nbsp;否</label></td>
						</tr>
					</tbody>
				</table>
			</form>
		</div>
	</div>
</div>